package view.jfreechartTest;

import java.awt.*;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import view.DrawXYFromDatabase;

public class XYBarChartDemo1 extends ApplicationFrame
{
	private static IntervalXYDataset intervalxydataset;
	
    public static IntervalXYDataset getIntervalxydataset() {
		return intervalxydataset;
	}

	public static void setIntervalxydataset(IntervalXYDataset intervalxydataset) {
		XYBarChartDemo1.intervalxydataset = intervalxydataset;
	}

	public XYBarChartDemo1(String chartName, IntervalXYDataset intervalxydataset)
    {
        super(chartName);
        this.intervalxydataset = intervalxydataset;
        JPanel jpanel = createDemoPanel(chartName);
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static JFreeChart createChart(String chartName)
    {
        JFreeChart jfreechart = ChartFactory.createXYBarChart(chartName, "日期", true, "金额", intervalxydataset, PlotOrientation.VERTICAL, true, false, false);
        //jfreechart.addSubtitle(new TextTitle("Source: http://www.amnestyusa.org/abolish/listbyyear.do", new Font("Dialog", 2, 10)));
        jfreechart.setBackgroundPaint(Color.gray);
        XYPlot xyplot = (XYPlot)jfreechart.getPlot();
        XYItemRenderer xyitemrenderer = xyplot.getRenderer();
        StandardXYToolTipGenerator standardxytooltipgenerator = new StandardXYToolTipGenerator("{1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0"));
        xyitemrenderer.setBaseToolTipGenerator(standardxytooltipgenerator);
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setRangeGridlinePaint(Color.white);
        
        TextTitle t = jfreechart.getTitle();
		t.setFont(new Font("宋体", Font.BOLD, 40));// 标题文字
		LegendTitle legend = jfreechart.getLegend();
		legend.setItemFont(new Font("黑体", Font.BOLD, 11));// 图例文字
        
        DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
        dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        dateaxis.setLabelFont(new Font("宋体", Font.BOLD, 20));
        dateaxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));
        dateaxis.setLowerMargin(0.01D);
        dateaxis.setUpperMargin(0.01D);
        
        ValueAxis numberaxis = xyplot.getRangeAxis(); 
        numberaxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// y轴标题
		numberaxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// y轴坐标上文字
        return jfreechart;
    }

//    private static IntervalXYDataset createDataset()
//    {
//    	String viewName = "merge_recharge";
//    	return DrawXYFromDatabase.drawXYPlotFromViewSet(viewName,"充值金额");
//    	
//    }

    public static JPanel createDemoPanel(String chartName)
    {
        return new ChartPanel(createChart(chartName));
    }

    public static void main(String args[])
    {
//        XYBarChartDemo1 xybarchartdemo1 = new XYBarChartDemo1("State Executions - USA");
//        xybarchartdemo1.pack();
//        RefineryUtilities.centerFrameOnScreen(xybarchartdemo1);
//        xybarchartdemo1.setVisible(true);
    }
}
